package com.gl.emms.whale.ui;

import com.gl.emms.whale.R;
import com.gl.emms.whale.app.WhaleMonitorActivity;
import com.gl.emms.whale.util.PlayerUtile;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.SeekBar;

/**	宣传片播放器界面类
 * @author ganlinux
 *
 */
public class PlayerActivity extends WhaleMonitorActivity{ 
	private SurfaceView surfaceView; 
	private Button btnPause, btnPlayUrl, btnStop; 
	private SeekBar skbProgress; 
	private PlayerUtile player; 
	private LinearLayout component;

	/** Called when the activity is first created. */ 
	@Override 
	public void onCreate(Bundle savedInstanceState) { 
		super.onCreate(savedInstanceState); 
		requestWindowFeature(Window.FEATURE_NO_TITLE);// 去掉标题栏
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
		setContentView(R.layout.activity_player); 
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 

		initViews();
	} 
	@Override
	public void initViews() {
		// TODO Auto-generated method stub
		surfaceView = (SurfaceView) this.findViewById(R.id.surfaceView1); 
		surfaceView.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				if (component.getVisibility()==View.INVISIBLE) {
					component.setVisibility(View.VISIBLE);
				}else
				{
					component.setVisibility(View.INVISIBLE);
				}
				return false;
			}
		});
		component = (LinearLayout) findViewById(R.id.component);

		btnPlayUrl = (Button) this.findViewById(R.id.btnPlayUrl); 
		btnPlayUrl.setOnClickListener(new ClickEvent()); 

		btnPause = (Button) this.findViewById(R.id.btnPause); 
		btnPause.setOnClickListener(new ClickEvent()); 

		btnStop = (Button) this.findViewById(R.id.btnStop); 
		btnStop.setOnClickListener(new ClickEvent()); 

		skbProgress = (SeekBar) this.findViewById(R.id.skbProgress); 
		skbProgress.setOnSeekBarChangeListener(new SeekBarChangeEvent()); 
		player = new PlayerUtile(surfaceView, skbProgress); 
	}
	/**点击事件类
	 * @author ganlinux
	 *
	 */
	class ClickEvent implements OnClickListener { 

		@Override 
		public void onClick(View arg0) { 
			if (arg0 == btnPause) { 
				player.pause(); 
			} else if (arg0 == btnPlayUrl) { 
				player.playUrl("http://ganlinux.qiniudn.com/wj_soho.mp4"); 
			} else if (arg0 == btnStop) { 
				player.stop(); 
			} 

		} 
	} 

	/**进度类
	 * @author ganlinux
	 *
	 */
	class SeekBarChangeEvent implements SeekBar.OnSeekBarChangeListener { 
		int progress; 

		@Override 
		public void onProgressChanged(SeekBar seekBar, int progress, 
				boolean fromUser) { 
			// ԭ����(progress/seekBar.getMax())*player.mediaPlayer.getDuration() 
			this.progress = progress * player.mediaPlayer.getDuration() 
					/ seekBar.getMax(); 
		} 

		@Override 
		public void onStartTrackingTouch(SeekBar seekBar) { 

		} 

		@Override 
		public void onStopTrackingTouch(SeekBar seekBar) { 
			// seekTo()�Ĳ����������ӰƬʱ������֣�������seekBar.getMax()��Ե����� 
			player.mediaPlayer.seekTo(progress); 
		} 
	}



} 